package yanc.app

import grails.transaction.Transactional
import org.h2.util.New
import org.springframework.beans.factory.annotation.Value

@Transactional
class TypesService {
    @Value('${branchService.url}')
    protected String PATH

    @Value('${commService.urls}')
    protected String URLS

    def add(Types branch, String imgUrl, String title) {
        UUID uuid = UUID.randomUUID()
        branch.id = uuid
        branch.url = PATH + branch.id
        branch.createDate = new Date()
        branch.modifyDate = new Date()
        branch.imgUrl = URLS + imgUrl
        def comm = News.findById(title)
        branch.title = comm.title
        branch.news = comm
        branch.insert()

    }

    def del(String id) {
        def branch = Types.findById(id)
        branch.delete()
    }

    def update(Types branch, String imgUrl,String title) {
        if (branch.imgUrl != imgUrl) {
            branch.imgUrl = URLS + imgUrl
        }
        if (branch.title != title) {
            def comm = News.findById(title)
            branch.title = comm.title
            branch.news = comm
        }
        branch.modifyDate = new Date()
        branch.save()
        if (!branch.save()) {
            branch.errors.allErrors.each {
                println it
            }
        }
    }
}
